package com.atguigu.day2;


import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Resource1 {
    /*版本一 synchronize + wait notify*/
    int number = 0;

    public synchronized void increase() throws InterruptedException {
        //1、判断
        while (number != 0) {
            this.wait();
        }
        //2、干活
        System.out.println(Thread.currentThread().getName() + "----->" + (++number));
        //3、通知
        this.notifyAll();
    }

    public synchronized void decrease() throws InterruptedException {
        //1、判断
        while (number == 0) {
            this.wait();
        }
        //2、干活
        System.out.println(Thread.currentThread().getName() + "----->" + (--number));

        //3、通知
        this.notifyAll();
    }
}

class Resource2 {
    /*版本二 JUC,Lock + Condition.await + signalALl*/
    int number = 0;
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public void increase() throws InterruptedException {
        lock.lock();
        try {
            //1、判断
            if (number != 0) {
                condition.await();
            }

            //2、干活
            System.out.println(Thread.currentThread().getName() + "----->" + (++number));
            //3、通知
            condition.signalAll();
        } finally {
            lock.unlock();
        }


    }

    public synchronized void decrease() throws InterruptedException {

        lock.lock();
        try {
            //1、判断
            if (number == 0) {
                condition.await();
            }
            //2、干活
            System.out.println(Thread.currentThread().getName() + "----->" + (--number));

            //3、通知
            condition.signalAll();
        } finally {
            lock.unlock();
        }


    }
}
/*口诀一 ：高内低耦前提下 线程操作资源类*/
/*口诀二： 判断 干活 通知*/

/*
 * * 有一个初始值为零的变量，现有两个线程对该变量操作，
 * 实现一个线程对变量加1，实现一个线程对变量减1,交替来10轮
 * */
public class ProdConsumer {
    public static void main(String[] args) {
        Resource2 resource = new Resource2();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    resource.increase();
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "A").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    resource.decrease();
                    TimeUnit.MILLISECONDS.sleep(200);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "B").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    resource.increase();
                    TimeUnit.MILLISECONDS.sleep(300);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "C").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    resource.decrease();
                    TimeUnit.MILLISECONDS.sleep(400);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "D").start();

    }
}
